<?php 
/**
* PXFModel
*
*/
class PXFModel
{
	private static $models = array();
	private $fields = array();
	public $relations = array();
	protected $languages = null;

	public function __construct($lang_support)
	{	
		$inspector = PXFDBInspector::getInstance();
		$fields = $inspector->getTable($this->__toString());

		if($lang_support)
		{
			if(!is_null($this->languages))
			{
				foreach($this->languages as $lang)
				{
					foreach($fields as $key => $field)
					{
						$name = substr($field,0,strlen($field) - 3);
						if($name == $lang)
						{ 
							$current = sprintf('%s_%s',$lang,PXFInstance::$lang);
							if(strcmp($field,$current) != 0) unset($fields[$key]);
						}
					}
				}
			}
		}

		$this->fields = $fields;
		PXFModel::registerModel($this);
	}

	public function getNew()
	{
		return $this->instanceByArray(array());
	}

	public function find($id)
	{
		$condition = array('condition' => new PXFCondition('id',$id));
		return $this->findBy($condition,true);
	}

	public function findAll()
	{
		return $this->findBy();
	}

	public function findBy($args = array(),$first = false)
	{
		$db = PXFDataBase::getInstance();
		$query  = PXFSQLBuilder::select($this,$this->fields);
		$query .= PXFSQLBuilder::parseArgs($args); 
		$records = array();
		$result = $db->query($query);
		if($first)
		{
			if($result)
			{
				if($fields = $db->fetch($result))
				{
					return new PXFRecord($this,PXFSQLBuilder::undoPrepareFields($this,$fields));
				}
				else
				{
					return null;
				}
			}
		}
		else
		{
			while($fields = $db->fetch($result))
			{
				$records[] = new PXFRecord($this,PXFSQLBuilder::undoPrepareFields($this,$fields));
			}
			return $records; 
		}
		return null;
	}

	public function updateBy($fields,$args = array())
	{
		$db = PXFDataBase::getInstance();
		$query  = PXFSQLBuilder::update($this,$fields);
		$query .= PXFSQLBuilder::parseArgs($args); 
		$records = array();
		$result = $db->query($query);
	}

	public function findByQuery($query) 
	{
		$db = PXFDataBase::getInstance();
		$result = $db->query($query);
		$records = array();
		while($fields = $db->fetch($result))
		{
			$records[] = new PXFRecord($this,PXFSQLBuilder::undoPrepareFields($this,$fields));
		}
		return $records; 
	}

	public function instanceByQuery($query)
	{
		$db = PXFDataBase::getInstance();
		$result = $db->query($query);
		$fields = $db->fetch($result);
		$fields = PXFSQLBuilder::undoPrepareFields($this,$fields);
		return $result ? new PXFRecord($this,$fields) : null;
	}

	public function instanceByArray($fields)
	{
		$attrs = array();
		foreach($this->fields as $field)
		{
			$attrs[$field] = array_key_exists($field,$fields) ? $fields['field'] :  null;
		}	
		return new PXFRecord($this,$attrs);
	}

	public function __toString()
	{
		return strtolower(get_class($this));
	}

	public function getFields()
	{
		return $this->fields;
	}

	private static function registerModel($model)
	{
		PXFModel::$models[$model->__toString()] = $model;
	}

	public function __clone()
	{
		die('Please do not clone this object');
	}

	public function count($args = array())
	{
		$db = PXFDataBase::getInstance();
		$query = PXFSQLBuilder::count($this);
		$query .= PXFSQLBuilder::parseArgs($args);
		$result = $db->select($query); 
		$count = $db->fetch($result);
		return $count['total'];
	}

	public static function getModel($model)
	{
		$new_model = new $model(true);
		return $new_model;
		if(!in_array($model,PXFModel::$models) && class_exists($model))
		{
			$new_model = new $model(true);
			PXFModel::registerModel($new_model);
		}
		return PXFModel::$models[sprintf('%s',$model)];
	}

	public static function getModelNLS($model) // No Language support
	{
		$new_model = null;
		if(!in_array($model,PXFModel::$models) && class_exists($model))
		{
			$new_model = new $model(false);
		}
		return $new_model;
	}
}
?>
